/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cart;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
 *
 * @author Sam WONG
 */
public class chkValidUserId implements Serializable{
  private String userid;
    private int resultnumber;
    private String registedname[];
    private boolean similarChkValidFlag;
    private boolean exactChkValidFlag;
    
    public chkValidUserId(String id){
        userid = id;
        resultnumber = 0;
        similarChkValidFlag = false;
        exactChkValidFlag = false;
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception E){
        }
        try{
               Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eshop","root","");
               Statement Stmt = Conn.createStatement();
               ResultSet rs  = Stmt.executeQuery("SELECT * FROM member WHERE login_name LIKE '" + userid +"%';");
               while (rs.next()){
                   resultnumber++;
                   if (userid.equals(rs.getString("login_name")))
                       exactChkValidFlag = true;
               }
            if (resultnumber !=0){
                registedname = new String[resultnumber];
                similarChkValidFlag = true;
             }
         }catch(SQLException E){ } 
    }
    
    public boolean isValid(){
        if (!similarChkValidFlag) return true;
        else if (!exactChkValidFlag) return true;
        else return false;
    }

    public String[] getValidUserid() {
        String[] returnStr = new String[2];
        int count = 0;
        while (count<2){
            String str = userid;
            int rannum = (int) (Math.random()*20201);
            if(count==0) rannum *=2;
            else rannum = rannum*2 +1;
            boolean chkFlag =true;
            str += rannum;
            for(int i=0;i<resultnumber;i++){
                if (str.equals(registedname[i]))
                    chkFlag = false;
            }
            if (chkFlag){
                returnStr[count] = str;
                count++;
           }   
        }
        return returnStr;    
    }
}
